package zoran.wang;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import zoran.wang.entity.Person;
import zoran.wang.order.A;

import java.util.List;

/**
 * description：
 *
 * @author wangzhuo
 * @date 2022/3/16 22:24
 * @Email zoranwang@yeah.net
 */
public class SpringTest {

    ApplicationContext applicationContext;

    @Before
    public void beforeTest() {
        //获取Spring容器对象，由接口ApplicationContext表示，ClassPathXmlApplicationContext类路径src下
//        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //或者使用文件系统路径也可以
//        FileSystemXmlApplicationContext fileSystemXmlApplicationContext =
//                new FileSystemXmlApplicationContext("E:\\WorkSpace\\spring-demo\\spring-helloworld\\src\\applicationContext.xml");

    }

    /**
     * 通过id获取对象
     * 通过类型获取对象
     */
    @Test
    public void test01() {

        //从容器中获取Bean对象
        Person p1 = (Person) applicationContext.getBean("p1");

        System.out.println(p1);

//        Person p2 = applicationContext.getBean(Person.class);
//
//        System.out.println(p2);

    }


    /**
     * 通过id和类型获取对象
     */
    @Test
    public void test02() {

        Person person1 = (Person) applicationContext.getBean("p10");
        Person person2 = (Person) applicationContext.getBean("p10");

        System.out.println(person1);
        System.out.println(person2);

        Person person3 = (Person) applicationContext.getBean("p11");
        Person person4 = (Person) applicationContext.getBean("p11");

        System.out.println(person3);
        System.out.println(person4);

    }

    /**
     * 测试各种方式创建的对象
     */
    @Test
    public void test03() {

        A a = applicationContext.getBean("a1", A.class);

        System.out.println(a);

    }

    /**
     * 测试各种方式创建的对象
     */
    @Test
    public void test04() {

        zoran.wang.entity_2.Person p15 = applicationContext.getBean("p15", zoran.wang.entity_2.Person.class);

        System.out.println(p15);

    }

    /**
     * 测试各自动装配
     */
    @Test
    public void test05() {

        applicationContext = new ClassPathXmlApplicationContext("context-autowire.xml");

        zoran.wang.entity_2.Person person = applicationContext.getBean("person", zoran.wang.entity_2.Person.class);

        System.out.println(person);

    }

    /**
     * 测试后置处理器
     */
    @Test
    public void test06() {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("context-lifecycle.xml");

        ctx.close();

    }

}
